<template>
  <div class="comment_box">
    <h4>评论列表</h4>
    <hr>
    <textarea v-model="msg"></textarea>
    <mt-button type="primary" size="large" @click="postComment">发表评论</mt-button>
    <!-- 评论列表 -->
    <div class="comment-list" v-for="(item,i) in commentContentList" :key="item.add_time">
      <div
        class="comment-title"
      >第{{ i+1 }}楼&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;&nbsp;发表时间：{{ item.add_time | data}}</div>
      <div class="comment-content">{{ item.content === "undefined"?"这个家伙很懒，没有发表想说的话":item.content }}</div>
    </div>
    <mt-button type="danger" size="large" plain:true @click="addMore">查看更多</mt-button>
  </div>
</template>
<script>
import {Toast} from 'mint-ui'
export default {
  data() {
    return {
      page: 1,
      commentContentList: [],
      msg:""
    };
  },
  created() {
    this.getCommentContent();
  },
  props: ["id"],
  methods: {
    getCommentContent() {
      this.$http
        .get("api/getcomments/" + this.id + "?pageindex=" + this.page)
        .then(res => {
        //   console.log(res.body.message);
          if (res.body.status === 0) {
            this.commentContentList = this.commentContentList.concat(res.body.message);
          } else {
              Toast("加载失败")
          }
        });
    },
    addMore(){
        this.page++;
        this.getCommentContent()
    },
    postComment(){
      if(this.msg == "") {
        return Toast("评论内容不能为空")
      }
      this.$http.post("api/postcomment/" + this.id,{content:this.msg}).then(res=>{
        if(res.body.status === 0){
          Toast(res.body.message);
          // console.log(this.commentContentList);
          this.commentContentList.unshift({
            'add_time':Date.parse(new Date()),
            'content':this.msg,
            'user_name':"匿名用户"
          })
          
          
          this.msg = "";
          
        }
        
      })
    }
  }
};
</script>
<style lang="less">
.comment_box {
  textarea {
    height: 150px;
    margin-bottom: 0;
  }
  .comment-list {
    .comment-title {
      background-color: lightgrey;
      font-size: 13px;
      padding: 6px 4px;
    }
    .comment-content {
      padding: 6px 4px;
    }
  }
}
</style>